<template>
  <div class="BarChart chart" ref="el" style="width: 100%;height: 300px;"></div>
</template>

<script setup>
import { ref, nextTick, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
import "echarts/theme/fresh-cut" // echarts theme

const el = ref(null)

let chart = null

onMounted(() => {
  nextTick(() => initChart())
})

onBeforeUnmount(() => {
  if (!chart) return
  chart.dispose()
  chart = null
})

function initChart() {
  chart = echarts.init(el.value, 'fresh-cut')
  chart.setOption({
    tooltip: {
      trigger: 'axis',
      axisPointer: { // 坐标轴指示器，坐标轴触发有效
        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    grid: {
      top: 10,
      left: '2%',
      right: '2%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value',
      axisTick: {
        show: false
      }
    }],
    series: [{
      name: 'pageA',
      type: 'bar',
      stack: 'vistors',
      barWidth: '60%',
      data: [345, 234, 567, 234, 390, 330, 98],
      animationDuration: 2000
    }, {
      name: 'pageB',
      type: 'bar',
      stack: 'vistors',
      barWidth: '60%',
      data: [123, 345, 678, 89, 234, 24, 5],
      animationDuration: 2000
    }, {
      name: 'pageC',
      type: 'bar',
      stack: 'vistors',
      barWidth: '60%',
      data: [30, 52, 200, 334, 390, 330, 220],
      animationDuration: 2000
    }]
  })

}
</script>

<style>
</style>